#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "functions.h"

static int isSquare (int); 

void printDouble (double *temp) {
	printf ("%5.1f ", *temp);
}

void setDouble (double *temp) {
	*temp = rand () % 100 * 1.1;
}

void printInt (int *temp) {
     printf ("%5d ", *temp);
}

void setInt (int *temp) { 
     *temp = rand () % 100; 
}

void divInt (int *temp) { 
	*temp = *temp/2; 
}

void isFib (int *temp) { 
     int i = 0, f = 0; 
     
     if (*temp == 0 || *temp == 1) 
         return;
     
     for (i = 2; f < *temp; i++)
         if ((f = fib (i)) == *temp) 
              return; 
     
     *temp = 0; 
}

/*N is a Fibonacci number if and only if (5*N*N + 4) or (5*N*N - 4) is a perfect square!*/

void isFib2 (int *temp) { 
     if (isSquare (5 * *temp * *temp + 4))
         return;
     if (isSquare (5 * *temp * *temp -4))
         return;
     *temp = 0; 
}

long long fib (int n) {
       long long f[2]; int i;
       f[0] = f[1] = 1;

       for (i = 2; i <= n; i++)
           f[i%2] = f[(i-1)%2] + f[(i-2)%2];

       return f[n%2];
}

static int isSquare (int n) {
    double root = sqrt(n);
    
    if (n == (int) (root) * (int) (root)) 
       return 1;
    else 
       return 0;
}


